package net.xuele.learn.interrupt;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class TestInterrupt {

    public static void main(String[] args) throws InterruptedException {
        RunTask runTask = new RunTask();
//        runTask.getQueue().add(1);
//        runTask.getQueue().add(2);
        Thread thread = new Thread(runTask);

        thread.start();


        Thread.sleep(2000);
        System.out.println("start ....");
        thread.interrupt();
        System.out.println(thread.getName() + "====" + thread.isInterrupted());


        Thread.sleep(4000);
        thread.interrupt();
        System.out.println(thread.getName() + "====" + thread.isInterrupted());

//        RunTaskWithBlock runTaskWithBlock = new RunTaskWithBlock();
//        Thread thread = new Thread(runTaskWithBlock);
//
//        thread.start();
//
//        Thread.sleep(1000);
//
//        thread.interrupt();
    }


    static class RunTask implements Runnable {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(2);

        public BlockingQueue getQueue() {
            return queue;
        }

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(10000);
//                    queue.put(3);
                } catch (InterruptedException e) {
                    System.out.println("====== " + Thread.currentThread().getName() + "----" + Thread.currentThread().isInterrupted());
//                    Thread.currentThread().interrupt();
//                    Thread.interrupted();
                }
                System.out.println(System.currentTimeMillis() + " ======>okkkkkkkkk");
            }
        }
    }

    static class RunTaskWithBlock implements Runnable {
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("xxxx");
                System.out.println("====== " + Thread.currentThread().getName() + "----" + Thread.currentThread().isInterrupted());
                System.out.println(Thread.interrupted());
            }
        }
    }
}
